package com.tz;

import com.tz.crm.BaseTest;
import com.tz.crm.dao.AddressDao;
import com.tz.crm.dao.IndexpictureDao;
import com.tz.crm.entity.*;
import com.tz.crm.entity.enumeration.*;
import com.tz.crm.service.*;
import com.tz.xml.AreaDomParser;
import org.fluttercode.datafactory.impl.DataFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Test
@SuppressWarnings("ALL")
public class InitData extends BaseTest {

    @Autowired
    private AreaService areaService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ContactService contactService;
    @Autowired
    private VisitTypeService visitTypeService;
    @Autowired
    private VisitRecordService visitRecordService;
    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private GlobalConfigService globalConfigService;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private IndexpictureDao indexpictureDao;

    @Test(priority = 1)
    public void initArea() {
        AreaDomParser ad = new AreaDomParser();
        List<Area> areas = ad.getAreaFromXML("com/tz/xml/area.xml");
        for (Area a : areas) {
            Area area = new Area();
            area.setName(a.getName());
            area.setCode(a.getCode());
            area.setLevel(1);
            areaService.save(area);
            for (Area c : a.getChildren()) {
                Area area1 = new Area();
                area1.setName(c.getName());
                area1.setCode(c.getCode());
                area1.setLevel(2);
                area1.setParent(area);
                areaService.save(area1);
            }
        }
    }

    @Test(priority = 2)
    public void initDepartment() {
        Department d1 = new Department();
        d1.setDeptName("天智教育");
        departmentService.save(d1);

        Department d2 = new Department();
        d2.setDeptName("总经理室");
        d2.setParent(d1);
        departmentService.save(d2);

        Department d4 = new Department();
        d4.setDeptName("渠道部");
        d4.setParent(d1);
        departmentService.save(d4);

        Department d6 = new Department();
        d6.setDeptName("市场部");
        d6.setParent(d1);
        departmentService.save(d6);

        Department d8 = new Department();
        d8.setDeptName("职业发展部");
        d8.setParent(d1);
        departmentService.save(d8);

        Department d10 = new Department();
        d10.setDeptName("财务部");
        d10.setParent(d1);

        Department d16 = new Department();
        d16.setDeptName("员工部");
        d16.setParent(d10);

        Department d17 = new Department();
        d17.setDeptName("学员部");
        d17.setParent(d10);
        departmentService.save(d10);
        departmentService.save(d16);
        departmentService.save(d17);
    }

    @Test(priority = 3)
    public void initRole() {
        Role r1 = new Role();
        r1.setValue("salesman");
        r1.setName("业务员");
        r1.setRemark("customer");
        r1.setSeq(1);
        roleService.save(r1);

        Role r2 = new Role();
        r2.setValue("salesManager");
        r2.setName("业务经理");
        r2.setRemark("customer,报表管理");
        r2.setSeq(2);
        roleService.save(r2);

        Role r3 = new Role();
        r3.setValue("account");
        r3.setName("财务");
        r3.setRemark("合同管理");
        r3.setSeq(3);
        roleService.save(r3);

        Role r4 = new Role();
        r4.setValue("admin");
        r4.setName("管理员");
        r4.setRemark("customer,报表管理,合同管理,系统设置");
        r4.setSeq(4);
        roleService.save(r4);
    }

    @Test(priority = 4)
    public void initUser() {
        Role admin = roleService.get("value", "admin");
        saveByNameAndRole("admin", admin);

        Role salesman = roleService.get("value", "salesman");
        saveByNameAndRole("sales", salesman);

        Role salesManager = roleService.get("value", "salesManager");
        saveByNameAndRole("manager", salesManager);

        Role account = roleService.get("value", "account");
        saveByNameAndRole("account", account);
    }

    @Test(priority = 5)
    public void initCustomer() {
        DataFactory dataFactory = new DataFactory();
        List<User> userList = userService.getAll();
        Level[] levels = Level.values();
        Title[] titles = Title.values();
        Source[] sources = Source.values();
        Status[] statuses = Status.values();
        List<Area> provinces = areaService.getByLevel(1);

        Customer c = new Customer();
        c.setOwner(userList.get(0));
        c.setName("tom");
        c.setStatus(Status.UNSIGNED);
        c.setType(Type.ENTERPRISE);
        c.setLevel(Level.TWO);
        c.setSource(Source.VISIT);
        customerService.save(c);

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j <= i; j++) {
                Customer customer = new Customer();
                customer.setName("Coco");
                customer.setAddress("苏州");
                customer.setAddress2("安徽");
                customer.setBusiness("");
                customer.setFax("");
                customer.setLevel(levels[dataFactory.getNumberBetween(0,
                        levels.length - 1)]);
                customer.setSource(sources[dataFactory.getNumberBetween(0,
                        sources.length - 1)]);
                customer.setStatus(statuses[dataFactory.getNumberBetween(0,
                        statuses.length - 1)]);
                customer.setOwner(userList.get(dataFactory.getNumberBetween(0,
                        userList.size() - 1)));
                customer.setWebSite("www."
                        + dataFactory.getRandomWord().toLowerCase() + ".com");
                customer.setType(Type.ENTERPRISE);
                String provinceName = provinces.get(
                        (int) (Math.random() * (provinces.size()))).getName();
                customer.setProvinceName(provinceName);
                Calendar createDate = Calendar.getInstance();
                createDate.set(createDate.get(Calendar.YEAR), createDate.get(Calendar.MONTH)
                        - j, 1, 24, 00);
                customer.setCreateDate(createDate.getTime());
                customerService.save(customer);

                Contact contact = new Contact();
                contact.setCustomer(customer);
                contact.setEmail(dataFactory.getEmailAddress());
                contact.setGender(dataFactory.chance(70) ? Gender.MALE
                        : Gender.FEMALE);
                contact.setMajor(j == 0);
                contact.setMobilePhone(String.valueOf(dataFactory
                        .getNumberBetween(100000, 999999)));
                contact.setName(dataFactory.getName());
                contact.setPhone(String.valueOf(dataFactory
                        .getNumberBetween(100000, 999999)));
                contact.setTitle(titles[dataFactory.getNumberBetween(0,
                        titles.length - 1)]);
                contact.setQq(String.valueOf(dataFactory.getNumberBetween(
                        100000, 999999)));
                contact.setRemark(dataFactory.getRandomText(10, 30));
                contactService.save(contact);

        /*DataFactory dataFactory = new DataFactory();
        Level[] levels = Level.values();
        Customer.Source[] sources = Customer.Source.values();
        Customer.Status[] statuses = Customer.Status.values();
        Customer.Type[] custTypes = Customer.Type.values();
        Contact.Title[] titles = Contact.Title.values();
        List<User> userList = userService.getAll();

        List<Area> provinces = areaService.getByLevel(1);
        if (provinces != null && provinces.size() > 0) {
            Customer customer = new Customer();
            customer.setAddress("addr");
            customer.setAddress2("");
            customer.setBusiness("");
            customer.setFax("");
            customer.setLevel(levels[dataFactory.getNumberBetween(0,
                    levels.length - 1)]);
            customer.setName("tom");
            customer.setRemark("");
            customer.setSource(sources[dataFactory.getNumberBetween(0,
                    sources.length - 1)]);
            customer.setStatus(statuses[dataFactory.getNumberBetween(0,
                    statuses.length - 1)]);
            customer.setOwner(userList.get(dataFactory.getNumberBetween(0,
                    userList.size() - 1)));
            customer.setWebSite("www."
                    + dataFactory.getRandomWord().toLowerCase() + ".com");
            customer.setType(custTypes[dataFactory.getNumberBetween(0, 1)]);
            String provinceName = provinces.get(
                    (int) (Math.random() * (provinces.size()))).getName();
            List<Area> citys = areaService
                    .getAreaWithChildren(provinceName);
                *//*String cityName = citys.get(
                        (int) (Math.random() * (citys.size()))).getName();*//*
            customer.setProvinceName(provinceName);
            //customer.setCityName(cityName);
            customerService.save(customer);

            Contact contact = new Contact();
            contact.setCustomer(customer);
            contact.setEmail(dataFactory.getEmailAddress());
            contact.setGender(dataFactory.chance(70) ? Gender.MALE
                    : Gender.FEMALE);
            //contact.setMajor(j == 0);
            contact.setMobilePhone(String.valueOf(dataFactory
                    .getNumberBetween(100000, 999999)));
            contact.setName(dataFactory.getName());
            contact.setPhone(String.valueOf(dataFactory
                    .getNumberBetween(100000, 999999)));
            contact.setTitle(titles[dataFactory.getNumberBetween(0,
                    titles.length - 1)]);
            contact.setQq(String.valueOf(dataFactory.getNumberBetween(
                    100000, 999999)));
            contact.setRemark(dataFactory.getRandomText(10, 30));
            contactService.save(contact);*/
            }
        }

    }

    @Test(priority = 6)
    public void initVisitType() {
        VisitType vt1 = new VisitType();
        vt1.setValue("home");
        vt1.setName("上门拜访");
        vt1.setReq(1);
        vt1.setValid(true);
        visitTypeService.save(vt1);

        VisitType vt2 = new VisitType();
        vt2.setValue("telephone");
        vt2.setName("电话拜访");
        vt2.setReq(2);
        vt2.setValid(true);
        visitTypeService.save(vt2);

        VisitType vt3 = new VisitType();
        vt3.setValue("email");
        vt3.setName("邮件拜访");
        vt3.setReq(3);
        vt3.setValid(true);
        visitTypeService.save(vt3);

        VisitType vt4 = new VisitType();
        vt4.setValue("fax");
        vt4.setName("传真");
        vt4.setReq(4);
        vt4.setValid(true);
        visitTypeService.save(vt4);

        VisitType vt5 = new VisitType();
        vt5.setValue("message");
        vt5.setName("短信");
        vt5.setReq(5);
        vt5.setValid(true);
        visitTypeService.save(vt5);

        VisitType vt6 = new VisitType();
        vt6.setValue("walk_in");
        vt6.setName("客户上门");
        vt6.setReq(6);
        vt6.setValid(true);
        visitTypeService.save(vt6);
    }

    @Test(priority = 7)
    public void initVisitRecord() {
        DataFactory dataFactory = new DataFactory();
        List<Customer> customerList = customerService.getAll();
        List<VisitType> visitTypeList = visitTypeService.getAll();
        Level[] levels = Level.values();

        for (int i = 0; i < customerList.size(); i++) {
            Customer customer = customerList.get(i);
//            List<Contact> contactList = contactService.getByCustomerId(customer
//                    .getId());

            // 产生随机数量的拜访记录
            int maxVisitRecord = dataFactory.getNumberBetween(3, 20);

            for (int j = 0; j < maxVisitRecord; j++) {
                VisitRecord vr = new VisitRecord();
//                vr.setContact(contactList.get(dataFactory.getNumberBetween(0, 1)));
                vr.setCustomer(customer);
                vr.setFirstVisit(i == 0);
                vr.setOwner(customer.getOwner());
                Date minDate = new Date();
                minDate.setTime(new Long("1325383441272"));
                vr.setVisitDate(dataFactory.getDateBetween(minDate, new Date()));
                vr.setVisitType(visitTypeList.get(dataFactory.getNumberBetween(0, visitTypeList.size() - 1)));
                vr.setContent(dataFactory.getRandomText(10, 30));
                vr.setLevel(levels[dataFactory.getNumberBetween(0, levels.length - 1)]);
                visitRecordService.save(vr);
            }
        }
    }

    private void saveByNameAndRole(String name, Role role) {
        DataFactory dataFactory = new DataFactory();
        List<Department> departmentList = departmentService.getAll();

        User user = new User();
        user.getRoleSet().add(role);
        user.setEmpNo("EMP" + name.toUpperCase());
        user.setUsername(name);
        user.setPassword(name);
        user.setRealName(name.toUpperCase());
        user.setDept(departmentList.get(dataFactory.getNumberBetween(0,
                departmentList.size() - 1)));
        user.setBirthDate(dataFactory.getBirthDate());
        user.setGender(dataFactory.chance(70) ? Gender.MALE : Gender.FEMALE);
        user.setMaxCustomer(dataFactory.getNumberBetween(30, 60));
        user.setRemarks(dataFactory.getRandomText(1, 30));
        user.setValid(true);
        user.setTelephone(String.valueOf(dataFactory.getNumberBetween(100000,
                999999)));
        userService.save(user);
    }

    @Test(priority = 9)
    public void initAnnouncement() {
        Announcement ac = new Announcement();
        ac.setTitle("会议通知");
        ac.setContext("周二下午至会议室开销售部门会议");
        announcementService.save(ac);

        Announcement ac1 = new Announcement();
        ac1.setTitle("欢迎使用CRM v2.2");
        ac1.setContext("建议使用FF,IE7.0+等主流浏览器");
        announcementService.save(ac1);
    }

    @Test(priority = 10)
    public void initGlobalConfig() {
        GlobalConfig gc1 = new GlobalConfig();
        gc1.setCode("AUTO_BACK_TO_SEA");
        gc1.setDisplayName("自动归还公海");
        gc1.setValue("T");
        globalConfigService.save(gc1);

        GlobalConfig gc2 = new GlobalConfig();
        gc2.setCode("MAX_HOLD_DAYS");
        gc2.setDisplayName("持有最大天数 ");
        gc2.setValue("30");
        globalConfigService.save(gc2);

        GlobalConfig gc3 = new GlobalConfig();
        gc3.setCode("UNCONFLICT_TRANSMIT_ADD");
        gc3.setDisplayName("跳转新增用户 ");
        gc3.setValue("T");
        globalConfigService.save(gc3);

        GlobalConfig gc4 = new GlobalConfig();
        gc4.setCode("ALLUSER_TO_SEE");
        gc4.setDisplayName("所有用户可见");
        gc4.setValue("F");
        globalConfigService.save(gc4);
    }

    @Test(priority = 11)
    public void initAddress() {
        Address address1 = new Address(116.447675, 39.925781, "北京市朝阳区");
        Address address2 = new Address(117.265348, 31.856387, "安徽省合肥市蜀山区");
        Address address3 = new Address(120.601462, 31.304842, "江苏省苏州市姑苏区烽火路");
        List<Customer> customerList = customerService.getAll();
        Customer customer = customerList.get(0);
        address1.setCustomer(customer);
        address2.setCustomer(customer);
        address3.setCustomer(customer);
        addressDao.save(address1);
        addressDao.save(address2);
        addressDao.save(address3);
    }

    @Test(priority = 12)
    public void initIndexpicture() throws Exception {
        File file = new File("src/main/resources/public/images/login.jpg");
        File file1 = new File("src/main/resources/public/images/login2.jpg");
        File file2 = new File("src/main/resources/public/images/login3.jpg");
        indexpictureDao.upload(file,"登录");
        indexpictureDao.upload(file1,"登录2");
        indexpictureDao.upload(file2,"登录3");
    }

}
